//
//  RegisterView.m
//  CCJD
//
//  Created by foreveross－bj on 14/12/22.
//  Copyright (c) 2014年 foreveross－bj. All rights reserved.
//

#import "RegisterView.h"

#define kBASpace   20
#define kABLeft    10
#define kABWidth   15
#define kASSpace   5
#define kSLWidth   200
#define kSLHeight  15

@interface RegisterView ()

@property (nonatomic, strong) UIButton *isAgreeButton;   //是否同意条款按钮
@property (nonatomic, strong) UILabel *serverItemLabel; //点击进入服务条款页面

@end

@implementation RegisterView

- (id)initWithFrame:(CGRect)frame requestName:(NSString *)requestName
{
    self = [super initWithFrame:frame requestName:requestName];
    if (self) {
        // Initialization code
        CGRect rect = frame;
        rect.size.height = kPTHeight * 3 + kIHeight * 2 + kTISpace * 3 + kITSpace * 2 + kTBSpace + kLHeight + kBASpace + kABWidth;
        self.frame = rect;
        [self addRegisterView];
    }
    return self;
}

- (void)addRegisterView{
    //添加是否同意服务条款按钮
    self.isAgreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _isAgreeButton.frame = CGRectMake(kABLeft, kPTHeight * 3 + kIHeight * 2 + kTISpace * 3 + kITSpace * 2 + kTBSpace + kLHeight + kBASpace, kABWidth, kABWidth);
    [_isAgreeButton setBackgroundImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
    _isAgreeButton.selected = NO;
    [_isAgreeButton addTarget:self action:@selector(selectServerItem:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_isAgreeButton];
    
    //添加服务条款lebal
    self.serverItemLabel = [[UILabel alloc] initWithFrame:CGRectMake(kABLeft + kABWidth + kASSpace,  kPTHeight * 3 + kIHeight * 2 + kTISpace * 3 + kITSpace * 2 + kTBSpace + kLHeight + kBASpace, kSLWidth, kSLHeight)];
    _serverItemLabel.text = @"我已阅读并同意《服务条款》";
    _serverItemLabel.textColor = [UIColor whiteColor];
    _serverItemLabel.font = [UIFont systemFontOfSize:15];
    _serverItemLabel.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    UITapGestureRecognizer *serverLabelTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(readServerItem:)];
    [_serverItemLabel addGestureRecognizer:serverLabelTapGesture];
    _serverItemLabel.userInteractionEnabled = YES;
    [self addSubview:_serverItemLabel];
}
/**
 * 点击是否同意服务条款按钮执行此方法
 * @param button
 **/
- (void)selectServerItem:(UIButton *)button{
    if (button.selected == NO) {
        [button setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
        button.selected = YES;
    }else if(button.selected == YES) {
        [button setBackgroundImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        button.selected = NO;
    }
}
/**
 * 点击服务条款按钮执行此方法
 * @param tapGesture
 **/
- (void)readServerItem:(UITapGestureRecognizer *)tapGesture{
    if (_readServerItem) {
        _readServerItem();
    }
}

/**
 * 点击注册或修改密码按钮调用此方法。
 * @param tapGesture
 **/
- (void)doRequest:(UITapGestureRecognizer *)tapGesture{
    if (_doRegister) {
        _doRegister(self.phoneTextField.text, self.securityCodeTextField.text, self.passwordTextField.text, _isAgreeButton.selected);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
